<?php
namespace Home\Controller;
use Think\Controller;
use Org\Util\QQconnect;
use Org\Util\Wechatauth;
use Org\Util\SaeTOAuthV2;
//use Org\Util\SaeTClientV2;
header("Content-Type:text/html; charset=utf-8");
class LoginController extends Controller {


    //验证码图片
    function verify() {
        $config =    array(
            'fontSize'    =>    20,    // 验证码字体大小
            'length'      =>    4,     // 验证码位数
            'useCurve'    =>    false, // 关闭验证码杂点
            'useCurve' => false,
        );

        $Verify = new \Think\Verify($config);
        $Verify->codeSet = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
        $Verify->entry();
    }

    //验证用户名是否存在
    public function checkExist()
    {
        $username = trim(I('post.name'));
        $field    = I('post.field');
        $res = M('user')->where(array($field=>$username))->getField('id');
        if($res) echo 1; else echo 0;
    }

    public function login()
    {
//        if(check_login()){
//            $this->redirect('/Home/Index/index');
//        }else
            $this->display();
    }

    //登录验证
    public function loginYz()
    {
        $phone   =   trim(I('post.phone'));
        $pwd   =    trim(I('post.pwd'));
        $user     =   M('users')->where(array('phone'=>$phone))->find();
        if(!$user) {
            $this->error("该账号不存在");
        }elseif(md5($pwd) != $user['pwd']){
            $this->error("密码错误");
        }else{
            session('user_id',$user['id']);
            session('pwd',$pwd);
            $data['login_time'] = date('Y-m-d H:i:s');
            cookie("name",$user['phone']);
            cookie("Username",$user['phone']);
            cookie('login_time',time(),3600*1);//用户登录有效时间
            session('login_time',time());
            cookie("user_id",$user['id']);
            cookie("pass",$pwd);
            M('users')->where(array('phone'=>$phone))->save($data);
            $this->redirect('Index/index');
        }

    }

    //验证码验证
    public function check_verify()
    {
        $code = trim(I('post.verCode'));//验证码

        $verify = new \Think\Verify();
        $res = $verify->check($code);
        if(!$res){
            $res['tag'] =   0;
            $res['msg'] =   '验证码错误,请刷新验证码！';
        }else $res['tag'] = 1;
        echo json_encode($res);
    }

    //验证手机号
    public function phoneCheck()
    {
        $phone  =   I("get.phone");

        if(!isMobile($phone)){
            echo 'no';die;
        }
        $tag    =   I("get.tag");
        if($tag == '1'){ //验证是否重复
            $res    =   M("users")->where(array("phone"=>$phone,'id'=>array("neq",$this->user_id)))->getField("id");
            $res    =   !$res;
        }elseif($tag == '2'){ //验证是否本人手机号
            $getPhone    =   M("users")->where(array('id'=>$this->user_id))->getField("phone");
            if($getPhone  == $phone) $res = 1;
            else $res = 0;
        }
        else{ //验证是否存在
            $res    =   M("users")->where(array("phone"=>$phone))->getField("id");
        }
        echo $res;
        die;
    }

    //发送短信
    public function sendMsg()
    {
        $phone_num = I('post.phone');
        $randStr = str_shuffle('1234567890');
        if(!isMobile($phone_num)){
            echo "no";die;
        }

        $code = substr($randStr,0,6);
        $url='http://web.cr6868.com/asmx/smsservice.aspx?name=15067805376&pwd=C70D3FEB70094E4E6BB050847BCF&content='.urlencode('您的短信验证码为：').$code.'（10分钟内有效）&mobile='.$phone_num.'&sign='.urlencode('GHT').'&type=pt&extno=123';
        if(function_exists('file_get_contents')){
            $file_contents = file_get_contents($url);
        }else{
            $ch = curl_init();
            $timeout = 5;
            curl_setopt ($ch, CURLOPT_URL, $url);
            curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
            $file_contents = curl_exec($ch);
            curl_close($ch);
        }
        $_SESSION['phone_code']=$code;
        echo 1;
    }

    
    //分享
    public function share()
    {
        $p_id   =   session('user_id');
        $this->assign("p_id",$p_id);
        $this->display();
    }

    //注册
    public function register()
    {
        $this->display();
    }
    
    //注册发送验证码
    public function verification()
    {
        if($_POST){
            $phone  =   I("post.phone");
            $p_id   =   I("post.p_id");
            $yzm    =   I("post.yzm");
            if(empty($phone)) $this->error("操作有误");
//            if($yzm != $_SESSION['phone_code'] ) $this->error("验证码错误,请重新填写");

            $this->success('验证成功',"/Home/Login/password/p_id/{$p_id}/phone/{$phone}",0);

        }else {
            $p_id       =   I("get.p_id");
            $phone      =   I("get.phone");
            $this->assign('p_id',$p_id);
            $this->assign('phone',$phone);
            $this->display();
        }

    }

    public function password()
    {

        $this->display();
    }

    //注册操作
    public function regDo(){
        $data   =   I('post.');
        if(empty($data['student_id']) )    $this->error("信息不完整");
        if(empty($data['pwd'])) $this->error("密码不能为空");

        $data['pwd']  =   md5(I('post.pwd'));

        if($data['pwd'] != md5($data['pwd2'])) $this->error("两次密码不一致");

        if($data['student_id']){
            $if_student_id = M('users')->where(array('student_id'=>$data['student_id']))->getField('id');
            if($if_student_id) $this->error("学号已经注册过");
        }
        unset($data['pwd2']);
        if($data['p_id']){
            $l_id   =   M("users")->where(array('id'=>$data['p_id']))->getField("l_id");
            $data['l_id']   =   $l_id?$l_id:$data['p_id'];
        }
        $data['add_time']   =   getTime();
        $data['login_time'] =   getTime();
        $insert    =   M('users')->add($data);
        if($insert){

            session('user_id',$insert);
            cookie('user_id',$insert);
            cookie('name',$data['student_id']);
            cookie('pass',$data['pwd']);
            cookie('login_time',time(),3600*1);//用户登录有效时间
            session('login_time',time());
            $this->redirect('/Home/Index/index');
        }else  {
            $this->error("注册失败");
        }

    }


    //信息修改手机号修改
    public function phoneEditYz()
    {
        $phone = I('post.phone');
        $yzm   = I('post.yzm');
        $id     =   session('user_id');
        $if_phone = M('users')->where(array('phone'=>$phone,'id'=>array('neq',$id)))->getField('id');
        if($if_phone) die("<script>alert('手机号已存在');history.back(-1);</script>");
        M('users')->where(array('id'=>$id))->save(array('phone'=>$phone));
        if($yzm != $_SESSION['phone_code'] ) $this->error("验证码错误,请重新填写");
        $this->redirect('/Home/Index/personal_edit');
    }

    //忘记密码
    public function forget_pwd()
    {
        if($_POST){
            $phone = I('post.phone');
            $yzm   = I('post.yzm');
            $if_phone = M('users')->where(array('phone'=>$phone))->getField('id');
            if(!$if_phone) $this->error("手机号不存在");
//            if($yzm != $_SESSION['phone_code'] ) $this->error("验证码错误,请重新填写");
            $this->assign('phone',$phone);
            $this->redirect('/Home/Login/set_pwd/phone/'.$phone);
        }else $this->display();
    }

    //修改密码
    public function set_pwd()
    {
        if($_POST){
            $data = I('post.');
            $phone = M('users')->where(array('phone'=>$data['phone']))->getField('id');
            if( !$phone ){
                $this->error("账号不存在");
            }

            if($data['pwd'] != $data['pwd2']){
                $this->error("两次密码不一致");
            }

            $save['pwd'] = md5($data['pwd']);
            $res = M('users')->where(array('phone'=>$data['phone']))->save($save);
            if($res !== false){
                $this->success('修改成功,请登录',"/Home/Login/login");
            }
            else{
                $this->error("修改失败");
            }
        }else{
            $phone  =   I("get.phone");
            $this->assign("phone",$phone);
            $this->display();
        }
    }



    //退出
    public function quit()
    {
        session_destroy();
        cookie('user_id',null);
        cookie('name',null);
        cookie('pass',null);
        cookie('login_time',null);//用户登录有效时间
        $this->redirect('Login/login');
    }
}

